Skip to content

Fix potential NPE in SelectJdkToolchainMojo.getJdkHome() - #184

Closed
elharo wants to merge 1 commit into
apache:masterfrom
elharo:fix-npe-getJdkHome
Closed

Fix potential NPE in SelectJdkToolchainMojo.getJdkHome()#184
elharo wants to merge 1 commit into
apache:masterfrom
elharo:fix-npe-getJdkHome

Conversation

@elharo

@elharo elharo commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #168

Problem

The getJdkHome() method in SelectJdkToolchainMojo had an unchecked null chain that could throw NullPointerException:

return ((Xpp3Dom) toolchain.getModel().getConfiguration())
        .getChild("jdkHome")
        .getValue();

Each call could return null without a guard:

  1. getModel() could return null
  2. getConfiguration() could return null (causing NPE on cast)
  3. (Xpp3Dom) cast could throw ClassCastException
  4. getChild("jdkHome") could return null
  5. getValue() on null would throw NPE

This could occur during IfSame mode execution.

Fix

Added null guards at each step in the chain, returning null if any step is null or the wrong type. Also replaced System.lineSeparator() with \n in the error message for consistency.

…parator()

Fixes apache#168

The getJdkHome() method in SelectJdkToolchainMojo had an unchecked
null chain that could throw NullPointerException. Each step in the
chain (getModel(), getConfiguration(), getChild(), getValue()) is
now guarded against null.

Also replaces System.lineSeparator() with \n in the error message
for consistency.
@elharo elharo changed the title Fix potential NPE in getJdkHome() and use \n instead of System.lineSeparator() Fix potential NPE in SelectJdkToolchainMojo.getJdkHome() Jul 23, 2026
@elharo

elharo commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Closed - starting over with test-first development

@elharo elharo closed this Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential NPE in SelectJdkToolchainMojo.getJdkHome() from unchecked null chain

1 participant